package com.yc.jpyy.base.mobile.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.yc.jpyy.mobile.dao.BizDao;

/**
 * 授权后的回调请求处理
 * 
 * @author liufeng
 * @date 2013-11-12
 */
@Component("uploadImgServlet")
public class UploadImgServlet extends HttpServlet {
	private static final long serialVersionUID = -1847238807216447030L;
	
	@Autowired
	private BizDao bizDao;
	
	@Override
	public void init() throws ServletException {
		
//		ServletContext servletContext = this .getServletContext();   
//        
//        WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);   
//                  
//        bizDao = (BizDao)ctx.getBean("bizDao" );

		
	}
	

	public void doGet(HttpServletRequest request, HttpServletResponse resp)
			throws ServletException, IOException {

		doPost(request, resp);

	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html,charset=UTF-8");
        
        resp.getWriter().print("2");

   /**
		SmartUpload smartUpload = new SmartUpload();
		try {
			// 添加缓存
			smartUpload.initialize(this.getServletConfig(), req, resp);
			smartUpload.upload();
			com.jspsmart.upload.File smartFile = smartUpload.getFiles().getFile(0);

			if (!smartFile.isMissing()) {
				String path = getServletContext().getRealPath("");
				// 图片名称
				String saveFileName = path.substring(0 , path.lastIndexOf("\\") + 1) + Config.getStudentImg() + "/" + smartFile.getFileName();
				// 保存图片
				smartFile.saveAs(saveFileName, smartUpload.SAVE_PHYSICAL);
				// 得到学员ID
				String userId = smartFile.getFileName().substring(0,smartFile.getFileName().indexOf("_"));
				// 修改学员图片数据
				int i = bizDao.updateUserImage(smartFile.getFileName(), userId);
				// 修改失败
				if (i < 1){
					resp.getWriter().print("0");
					return ;
				}
				System.out.println(Config.getImageAddress() + smartFile.getFileName());
				resp.getWriter().print(Config.getImageAddress() + smartFile.getFileName());
			} else {
				resp.getWriter().print("0");
			}
		} catch (Exception e) {
			resp.getWriter().print("0");
			e.printStackTrace();
			resp.getWriter().print(e);
		}
          **/
        
        
        
        
	}
}
